<template>
    <el-card class="!border-none " shadow="never">
        
        <div class=" flex flex-1 flex-row">
            <el-form class="-mb-4 " :model="formData" inline>
                <el-form-item label="管理员账号">
                    <el-input
                        class="w-64"
                        v-model="formData.username"
                        clearable
                    >
                    
                    </el-input>
                </el-form-item>
                
                <el-form-item label="管理员名称">
                    <el-input
                        class="w-64"
                        v-model="formData.nickname"
                        clearable
                    >
                    </el-input>
                </el-form-item>
            </el-form>
            
            <div class="flex-1  flex flex-row-reverse">
                <el-button class="mx-2" type="primary" @click="resetOnePage">查询</el-button>
                <el-button class="mx-2" @click="resetParams">重置</el-button>
            </div>
        </div>
    </el-card>
    
    
    <el-card class="!border-none mt-5" shadow="never">
        
        <el-button v-perms="['system:menu:add']" type="primary" @click="handleAdd()">
            <template #icon>
                <icon name="el-icon-Plus"/>
            </template>
            新增
        </el-button>
        
        
        <el-table
            v-loading="pagerData.loading"
            ref="tableRef"
            class="mt-4"
            size="large"
            :data="pagerData.lists"
        >
            <el-table-column prop="id" label="ID" min-width="100"/>
            
            <el-table-column prop="name" label="名称" min-width="150"/>
            
            <el-table-column prop="remark" label="备注" min-width="150"/>
            
            <el-table-column prop="sort" label="排序" min-width="100"/>
            
            <el-table-column prop="member" label="管理员人数" min-width="120"/>
            
            <el-table-column prop="createTime" label="创建时间" min-width="180"/>
            
            <el-table-column label="操作" width="190" fixed="right">
                <template #default="{ row }">
                    <el-button
                        v-perms="['system:role:edit']"
                        link
                        type="primary"
                        @click="handleEdit(row)"
                    >
                        编辑
                    </el-button>
                    
                    
                    <el-button
                        v-perms="['system:role:edit']"
                        link
                        type="primary"
                        @click="handleAuth(row)"
                    >
                        权限设置
                    </el-button>
                    
                    <el-button
                        v-perms="['system:role:del']"
                        link
                        type="danger"
                        @click="handleDelete(row)"
                    >
                        删除
                    </el-button>
                
                
                </template>
            
            </el-table-column>
        </el-table>
    </el-card>
    
    <edit-popup v-if="showEdit" ref="editRef" @success="getPageLists" @close="showEdit = false"/>
    <auth-popup v-if="showAuth" ref="authRef" @success="getPageLists" @close="showAuth = false"/>


</template>

<script lang="ts" setup name="role">
import {usePagerManager} from "@/manager/base/PagerManager";
import EditPopup from "./edit.vue"
import AuthPopup from "./auth.vue"
import feedback from "@/utils/feedback";
import {getRoleByPageLists, roleDelete} from "@/api/system/SystemAuthRoleController";

const authRef = shallowRef<InstanceType<typeof AuthPopup>>()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const showAuth = ref(false)


/**
 * 获取分页数据
 */
const {pagerData, getPageLists, resetParams, resetOnePage} = usePagerManager({
    fetchFun: getRoleByPageLists
})


const formData = reactive({
    username: '',
    nickname: ''
    
})

/**
 * 新增角色
 */
async function handleAdd() {
    showEdit.value = true
    await nextTick()
    editRef.value?.open()
}


/**
 * 编辑数据
 * @param row
 */
async function handleEdit(row: any) {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(row)
}


/**
 * 权限设置
 */
async function handleAuth(row: any) {
    showAuth.value = true
    await nextTick()
    authRef.value?.open()
    authRef.value?.setFormData(row)
}


/**
 * 删除角色
 */
async function handleDelete(row: any) {
    await feedback.confirm('确定要删除？')
    
    await roleDelete({id: row.id}).then(() => {
        feedback.msgSuccess('删除成功')
        getPageLists()
    })
}


onMounted(() => {
    getPageLists()
})
</script>
